﻿namespace DXJoystick
{
    using Microsoft.DirectX.DirectInput;
    using Microsoft.Xna.Framework;
    using System;
    using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Sequential)]
    public struct DirectInputThumbSticks
    {
        private const float center = 32767.5f;
        public Vector2 Left;
        public Vector2 Right;
        public Vector2 Third;
        public bool HasLeft;
        public bool HasRight;
        public bool HasThird;
        public DirectInputThumbSticks(Device device)
        {
            JoystickState state = device.CurrentJoystickState;
            this.HasLeft = false;
            this.Left = Vector2.Zero;
            this.HasRight = false;
            this.Right = Vector2.Zero;
            this.HasThird = false;
            this.Third = Vector2.Zero;
            if (device.Caps.NumberAxes > 0)
            {
                this.HasLeft = true;
                this.Left = new Vector2((state.X - 32767.5f) / 32767.5f, (state.Y - 32767.5f) / 32767.5f);
                if (device.Caps.NumberAxes > 2)
                {
                    this.HasRight = true;
                    this.Right = new Vector2((state.Rz - 32767.5f) / 32767.5f, (state.Z - 32767.5f) / 32767.5f);
                    if (device.Caps.NumberAxes > 4)
                    {
                        this.HasThird = true;
                        this.Third = new Vector2((state.Rx - 32767.5f) / 32767.5f, (state.Ry - 32767.5f) / 32767.5f);
                    }
                }
            }
        }
    }
}

